﻿#include <QPainter>
#include <QPen>

#include "CPlcItemComment.h"

CPlcItemComment::CPlcItemComment(const QRectF &rect,
                                 const int8_t type,
                                 const QString &str,
                                 IPlcBaseItem *parent)
    : IPlcBaseItem(parent)
{
    mVariableNum  = str;
    mSelectActive = false;
    mSwitchOn     = false;
    mType         = type;
    mBranch       = NULL;
    setRect(rect);
}

CPlcItemComment::~CPlcItemComment()
{
    //如果当前为选中状态
    if (mSelectActive) setOnlySelectItem(NULL);
}

QRectF CPlcItemComment::getRect()
{
    return mRect;
}

void CPlcItemComment::setRect(const QRectF &rect)
{
    if (mRect == rect) return;
    //准备调整几何边界
    prepareGeometryChange();
    mRect         = rect;
    mBoundingRect = QRectF();
    // 缩放比例
    mdScale = mRect.width() / 65.0;
    //重绘
    update();
}

IPlcBaseItem *CPlcItemComment::getBranch()
{
    return mBranch;
}

void CPlcItemComment::setBranch(IPlcBaseItem *item)
{
    mBranch = item;
}

void CPlcItemComment::deleteBranch()
{
    mBranch = NULL;
}

void CPlcItemComment::setSelectActive(bool status)
{
    Q_UNUSED(status);
}

int8_t CPlcItemComment::getItemType()
{
    return mType;
}

void CPlcItemComment::setItemType(int8_t type)
{
    Q_UNUSED(type);
}

QString CPlcItemComment::getVariableNum()
{
    return mVariableNum;
}

void CPlcItemComment::setVariableNum(const QString &str)
{
    mVariableNum = str;
}

QRectF CPlcItemComment::boundingRect() const
{
    if (mBoundingRect.isNull()) mBoundingRect = mRect;

    return mBoundingRect;
}

void CPlcItemComment::paint(QPainter *painter,
                            const QStyleOptionGraphicsItem *option,
                            QWidget *widget)
{
    Q_UNUSED(option);
    Q_UNUSED(widget);

    // 设置反走样
    painter->setRenderHint(QPainter::Antialiasing, true);
    painter->setRenderHint(QPainter::TextAntialiasing, true);

    QBrush backGroundBrush(QColor(135, 206, 250));
    QBrush blueBrush(QColor(2, 167, 240));
    QPen pen;
    //设置画笔样式为实线
    pen.setStyle(Qt::DashLine);
    pen.setWidth(1);   //线宽1

    pen.setBrush(blueBrush);
    painter->setPen(pen);
    painter->setBrush(Qt::NoBrush);
    //绘制注释信息
    QFont font;
    font.setPixelSize(13);   //字体像素大小
    painter->setFont(font);
    painter->drawText(QRectF(mRect.left() + 3 * mdScale, mRect.top() + 5,
                             59 * mdScale, mRect.height()),
                      Qt::AlignLeft | Qt::TextWrapAnywhere, mVariableNum);
}

bool CPlcItemComment::getSelectActiveEnb()
{
    return false;
}

bool CPlcItemComment::getSelectActive()
{
    return mSelectActive;
}
